<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="工单编号" prop="warehousingNo">
				<el-input v-model="queryParams.warehousingNo" placeholder="请输入工单编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="档案区编号" prop="areaNo">
				<el-select v-model="queryParams.areaNo" placeholder="请选择档案区编号" style="width:230px;" @change="changeArea">
					<el-option v-for="item in areaOptions" :key="item.archAreaNo" :label="item.archAreaNo" :value="item.archAreaNo"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="档案柜编号" prop="cabinetNo">
				<el-select v-model="queryParams.cabinetNo" placeholder="请选择档案柜编号" style="width:230px;">
					<el-option v-for="item in cabinetOptions" :key="item.colNo" :label="item.colNo" :value="item.colNo"></el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item label="入库状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择入库状态">
					<el-option v-for="dict in stockin_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8
      ">
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="primary"-->
			<!--          plain-->
			<!--          icon="Plus"-->
			<!--          @click="handleAdd"-->
			<!--          v-hasPermi="['archive:storageTask:add']"-->
			<!--        >新增</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="success"-->
			<!--          plain-->
			<!--          icon="Edit"-->
			<!--          :disabled="single"-->
			<!--          @click="handleUpdate"-->
			<!--          v-hasPermi="['archive:storageTask:edit']"-->
			<!--        >修改</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="danger"-->
			<!--          plain-->
			<!--          icon="Delete"-->
			<!--          :disabled="multiple"-->
			<!--          @click="handleDelete"-->
			<!--          v-hasPermi="['archive:storageTask:remove']"-->
			<!--        >删除</el-button>-->
			<!--      </el-col>-->
			<!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['archive:storageTask:export']"
        >导出</el-button>
      </el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="storageTaskList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="序号" align='center' width="50">
				<template #default="scope">
					{{ scope.$index+1 }}
				</template>
			</el-table-column>
			<el-table-column label="工单编号" align="center" prop="warehousingNo" />
			<el-table-column label="入库状态" align="center" prop="status">
				<template #default="scope">
					<dict-tag :options="stockin_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<!-- <el-table-column label="档案区编号" align="center" prop="areaNo" />
			<el-table-column label="柜编号" align="center" prop="cabinetNo" /> -->
			<el-table-column label="操作员" align="center" prop="warehousingBy" />
			<el-table-column label="入库时间" align="center" prop="warehousingTime" width="160">
				<template #default="scope">
					<span>{{ parseTime(scope.row.warehousingTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" fixed="right" width="300">
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleView(scope.row, scope.index)" v-hasPermi="['archive:storageTask:edit']">详情</el-button>&nbsp;
					<el-button type="text" icon="Upload" v-if="scope.row.status != '03'" @click="submitStatus(scope.row, scope.index)"
					 v-hasPermi="['archive:storageTask:edit']">入库完成</el-button>

				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />
		<!-- 添加或修改档案入库任务对话框 -->
		<el-dialog :title="title" v-model="open" width="560px" append-to-body>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
		<!-- 查看详情 -->
		<el-dialog title="入库任务详情" v-model="openscan" width="90%" append-to-body>
			<el-form ref="storageTaskRef" :model="form" label-width="120px">
				<el-row>
					<el-col :span="24">
						<el-row>
							<el-col :span="6">
								<el-form-item label="工单编号" prop="warehousingNo">
									<el-input v-model="form.warehousingNo" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
							<!-- <el-col :span="6">
								<el-form-item label="档案区编号" prop="areaNo">
									<el-input v-model="form.areaNo" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="柜编号" prop="cabinetNo">
									<el-input v-model="form.cabinetNo" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col> -->
							<!-- 	<el-col :span="6">
								<el-form-item label="推送状态" prop="pushStatus">
									<el-select v-model="form.pushStatus" style="width:230px;" disabled="disabled">
										<el-option v-for="dict in push_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
									</el-select>
								</el-form-item>
							</el-col> -->
							<el-col :span="6">
								<el-form-item label="入库状态" prop="status">
									<el-select v-model="form.status" style="width:230px;" disabled="disabled">
										<el-option v-for="dict in stockin_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="操作员" prop="warehousingBy">
									<el-input v-model="form.warehousingBy" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="入库时间" prop="warehousingTime">
									<el-input v-model="form.warehousingTime" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
						</el-row>
					</el-col>
				</el-row>
				<el-divider content-position="center">入库明细数据信息</el-divider>
				<el-row>

					<el-col>
						<el-table :data="archiveRuKuList" ref="archiveStockIn" style="border:1px solid #dcdfe6;min-height: 550px;">
							<el-table-column label="档案区编号" width="150" align="center" prop="areaNo" />
							<el-table-column label="档案柜编号" align="center" prop="cabinetNo" width="150" />
							<el-table-column label="档案盒号" align="center" prop="boxNo" width="150" />
							<el-table-column label="档案盒位置" align="center" prop="storLoc" width="300" />
							<el-table-column label="借阅状态" align="center" prop="giveBackStatus">
								<template #default="scope">
									<dict-tag :options="borrow_flag" :value="scope.row.giveBackStatus" />
								</template>
							</el-table-column>
							<el-table-column label="档案名称" align="center" prop="archiveName" width="330" />
							<el-table-column label="档案类型" align="center" prop="archiveType" width="150">
								<template #default="scope">
									<dict-tag :options="archive_type" :value="scope.row.archiveType" />
								</template>
							</el-table-column>
							<el-table-column label="档案密级" align="center" prop="conLevel" width="100">
								<template #default="scope">
									<dict-tag :options="confd_lv" :value="scope.row.conLevel" />
								</template>
							</el-table-column>
							<el-table-column label="保管期限" align="center" prop="saveDate" width="100">
								<template #default="scope">
									<dict-tag :options="keep_dur" :value="scope.row.saveDate" />
								</template>
							</el-table-column>
							<el-table-column label="资料种类	" align="center" prop="meansType" width="150">
								<template #default="scope">
									<dict-tag :options="means_type" :value="scope.row.meansType" />
								</template>
							</el-table-column>
							<el-table-column label="介质类型" align="center" prop="medForm">
								<template #default="scope">
									<dict-tag :options="med_form" :value="scope.row.medForm" />
								</template>
							</el-table-column>
							<el-table-column label="对象类型" align="center" prop="archObjType" width="150">
								<template #default="scope">
									<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
								</template>
							</el-table-column>
							<el-table-column label="对象编号" align="center" prop="archObjNo" width="150" />
							<el-table-column label="对象名称" align="center" prop="archObjName" />
						</el-table>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="archiveRuKuListClose">关 闭</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="StorageTask">
	import {
		listStorageTask,
		getStorageTask,
		delStorageTask,
		addStorageTask,
		updateStorageTask,
		selectArchiveStorageBoxByTaskId,
		selectArchiveStorageByTaskBoxId,
		archiveStorageTaskResultReport,
		selectArchiveStorageByTaskId
	} from "@/api/archive/storageTask";
	import {
		listArchiveArchArea
	} from "@/api/archive/ArchiveArchArea";
	import {
		listArchiveCabinet
	} from "@/api/archive/ArchiveCabinet";
	const {
		proxy
	} = getCurrentInstance();

	const storageTaskList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const openscan = ref(false);
	const archiveBoxList = ref([]);
	const archiveRuKuList = ref([]);
	const areaOptions = ref([]);
	const cabinetOptions = ref([]);

	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			orgNo: null,
			roomNo: null,
			warehousingNo: null,
			houseNo: null,
			areaNo: null,
			cabinetNo: null,
			warehousingResult: null,
			pushStatus: null,
			status: null,
			creator: null,
			modifyTime: null,
			modifier: null
		},
		rules: {}
	});
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	const {
		queryParams,
		form,
		rules
	} = toRefs(data);
	const {
		push_status,
		execut_result,
		stockin_status,
		archive_type,
		save_extended,
		borrow_flag,
		keep_dur,
		confd_lv,
		med_form,
		means_type,
		arch_obj_type
	} = proxy.useDict('push_status', 'execut_result', 'stockin_status', 'archive_type', 'save_extended', 'borrow_flag',
		'keep_dur', 'confd_lv', 'med_form', 'means_type', 'arch_obj_type');

	/** 查询档案入库任务列表 */
	function getList() {
		loading.value = true;
		listStorageTask(queryParams.value).then(response => {
			storageTaskList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			taskId: null,
			orgNo: null,
			roomNo: null,
			warehousingNo: null,
			houseNo: null,
			areaNo: null,
			cabinetNo: null,
			warehousingResult: null,
			pushStatus: "0",
			status: "0",
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null
		};
		proxy.resetForm("storageTaskRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.taskId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加档案入库任务";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const taskId = row.taskId || ids.value
		getStorageTask(taskId).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改档案入库任务";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["storageTaskRef"].validate(valid => {
			if (valid) {
				if (form.value.taskId != null) {
					updateStorageTask(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addStorageTask(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	async function submitStatus(row) {
		const taskId = row.taskId;

		if (row.status == "03") {
			proxy.$modal.msgError('单据状态为【入库完成】，不需要重复点击！');
		} else {
			const response = await getStorageTask(taskId);
			form.value = response.data;

			if (form.value.taskId != null) {
				const updateResponse = await updateStorageTask(form.value);
				proxy.$modal.msgSuccess("入库完成");
				getList();
			}
		}
	}

	function submitInteface(row) {
		const warehousingNo = row.warehousingNo;
		if (row.status === "02") {
			proxy.$modal.msgError('单据状态为【入库中】，上报失败！');
		} else if (row.status === "03") {
			form.value.warehousingNo = warehousingNo;
			archiveStorageTaskResultReport(form.value).then(response => {
				proxy.$modal.msgSuccess("入库信息上报成功");
				getList();
			}).catch(error => {
				console.error("入库信息上报失败:", error);
			});
		} else {
			proxy.$modal.msgError('单据状态为【未完成】，上报失败！');
		}
	}
	/** 删除按钮操作 */
	function handleDelete(row) {
		const taskIds = row.taskId || ids.value;
		proxy.$modal.confirm('是否确认删除档案入库任务编号为"' + taskIds + '"的数据项？').then(function() {
			return delStorageTask(taskIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/storageTask/export', {
			...queryParams.value
		}, `入库任务_${new Date().getTime()}.xlsx`)
	}

	function resetList() {
		archiveBoxList.value = [];
		archiveRuKuList.value = [];
	}

	function handleView(row) {
		const taskId = row.taskId;
		getStorageTask(taskId).then(response => {
			form.value = response.data;
			openscan.value = true;
		});
		resetList();
		selectArchiveStorageByTaskId(taskId).then(response => {
			archiveRuKuList.value = response.data;
		});

	}

	function handleViewDetail(taskId, id) {
		selectArchiveStorageByTaskBoxId(taskId).then(response => {
			archiveRuKuList.value = response.data;
			//.filter(s=>s.taskBoxId==id);
		});
	}

	function archiveRuKuListClose() {
		archiveRuKuList.value = []
		openscan.value = false
	}
	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchArea().then(response => {
			areaOptions.value = response.rows;
		});
	};

	/** 查询档案柜下拉树结构 */
	function getListArchiveCabinet(query) {
		listArchiveCabinet(query).then(response => {

			cabinetOptions.value = response.rows;
		});
	};

	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchArea().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};

	/** 初始化档案柜数据 */
	function initCabinetOptions() {
		// 判断档案柜的数据是否存在，存在不获取，不存在则获取
		if (cabinetOptions.value.length === 0) {
			listArchiveCabinet().then(response => {
				cabinetOptions.value = response.rows;
			});
		}
	};

	getListArchiveArchArea();
	getListArchiveCabinet();
	getList();
</script>
